package com.leoman.spinningmachine.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.leoman.helper.listener.OnDlg1ParamListener;
import com.leoman.helper.util.LogUtil;
import com.leoman.spinningmachine.R;

/**
 * 对话框
 */
public class DialogUtils {

    private static DialogUtils instance;
    private Dialog alertDialog;

    public DialogUtils() {

    }

    public static DialogUtils getInstance() {
        if (instance == null) {
            LogUtil.json("instance为空");
            synchronized (SharedPreferencesUtils.class) {
                if (instance == null)
                    instance = new DialogUtils();
            }
        }
        return instance;
    }

    /**
     * 默认等待匡
     */
    public void show(Activity mContext) {
        if (mContext != null && !mContext.isFinishing()) {
            alertDialog = new Dialog(mContext, R.style.MyDialogStyle);
            Window window = alertDialog.getWindow();
            window.setGravity(Gravity.CENTER);
            window.setContentView(R.layout.dlg);
            alertDialog.setCanceledOnTouchOutside(false);
            alertDialog.show();
        }
    }

    /**
     * 取消
     */
    public void cancel() {
        if (alertDialog != null) {
            alertDialog.cancel();
        } else {
            LogUtil.json("为空");
        }
    }


    /**
     * 登录 注册 弹窗
     *
     * @param context
     * @param str
     */
    public void showResultDialog(Context context, String str) {
        if (context == null) {
            return;
        }
        alertDialog = new Dialog(context, R.style.MyDialogStyle);
        alertDialog.show();
        alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        alertDialog.getWindow().setGravity(Gravity.CENTER);
        alertDialog.getWindow().setWindowAnimations(R.style.bottomAnim);
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.getWindow().setContentView(R.layout.dialog_login);
        TextView dialogTitle = (TextView) alertDialog.getWindow().findViewById(R.id.tv_title);
        dialogTitle.setText(str);
        alertDialog.getWindow().findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        alertDialog.getWindow().findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
    }


    /**
     * 分享
     *
     * @param context
     * @param listener
     */
    public void share(Context context, OnDlg1ParamListener listener) {
        alertDialog = new Dialog(context, R.style.MyDialogStyle);
        alertDialog.show();
        alertDialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        alertDialog.getWindow().setGravity(Gravity.BOTTOM);
        alertDialog.getWindow().setWindowAnimations(R.style.bottomAnim);
        alertDialog.setCanceledOnTouchOutside(true);
        alertDialog.getWindow().setContentView(R.layout.dlg_share);

        ImageView iv_cancle = (ImageView) alertDialog.getWindow().findViewById(R.id.iv_cancle);  //取消按钮
        LinearLayout ll_wechat = (LinearLayout) alertDialog.getWindow().findViewById(R.id.ll_wechat);    //微信
        LinearLayout ll_qq = (LinearLayout) alertDialog.getWindow().findViewById(R.id.ll_qq);    //QQ
        LinearLayout ll_message = (LinearLayout) alertDialog.getWindow().findViewById(R.id.ll_message);  //短信

        iv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });

        ll_wechat.setTag(R.id.text1, alertDialog);
        ll_wechat.setTag(R.id.text3, listener);
        ll_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = (Dialog) v.getTag(R.id.text1);
                OnDlg1ParamListener listener = (OnDlg1ParamListener) v.getTag(R.id.text3);
                if (listener != null) {
                    listener.click("");
                }
                dialog.dismiss();
            }
        });

        ll_qq.setTag(R.id.text1, alertDialog);
        ll_qq.setTag(R.id.text3, listener);
        ll_qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = (Dialog) v.getTag(R.id.text1);
                OnDlg1ParamListener listener = (OnDlg1ParamListener) v.getTag(R.id.text3);
                if (listener != null) {
                    listener.click("");
                }
                dialog.dismiss();
            }
        });

        ll_message.setTag(R.id.text1, alertDialog);
        ll_message.setTag(R.id.text3, listener);
        ll_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dialog = (Dialog) v.getTag(R.id.text1);
                OnDlg1ParamListener listener = (OnDlg1ParamListener) v.getTag(R.id.text3);
                if (listener != null) {
                    listener.click("");
                }
                dialog.dismiss();
            }
        });

    }


}
